/// @tags: UnionFindSet UFS
#include <algorithm>
#include <cctype>
#include <cstdio>

typedef long long ll;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 2e5 + 5, mod = 1e9 + 7;
int n, f[N], siz[N];
bool selfcir[N], cir[N];
ll ans = 1;

int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("CF859E Desk Disorder.in", "r", stdin);
  freopen("CF859E Desk Disorder.out", "w", stdout);
#endif
#endif

  read(n);
  for (int i = 1; i <= 2 * n; i++) { f[i] = i, siz[i] = 1; }
  for (int i = 1, x, y; i <= n; i++) {
    read(x), read(y);
    if (x == y) selfcir[x] = true;
    x = find(x), y = find(y);
    if (x != y) {
      f[x] = y;
      selfcir[y] |= selfcir[x];
      siz[y] += siz[x];
    } else
      cir[x] = true;
  }
  for (int i = 1; i <= 2 * n; i++)
    if (find(i) == i && !selfcir[i])
      if ((ans *= (cir[i] ? 2 : siz[i])) >= mod) ans %= mod;
  printf("%lld\n", ans);
  return 0;
}